import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/ACCESS_ENUM";
import store from "@/store";
import router from "@/router";
import { reactive, ref, toRaw } from "vue";

// todo 完善
router.beforeEach(async (to, from, next) => {
  const needAccess = to?.meta?.access ?? ACCESS_ENUM.NOT_LOGIN;
  let loginUser = store.state.user.loginUser;
  if (needAccess === ACCESS_ENUM.NOT_LOGIN) {
    next();
  }

  // todo loginUser有值 loginUser.userRole为未定义
  if (!loginUser || loginUser.userRole === ACCESS_ENUM.FIRET) {
    await store.dispatch("user/getLoginUser");
    loginUser = store.state.user.loginUser;
    console.log("loginUser", loginUser.userRole);
  }
  if (loginUser.userRole === ACCESS_ENUM.NOT_LOGIN) {
    next(`/user/login?redirect = ${to.fullPath}`);
  }

  if (!checkAccess(store.state.user.loginUser, needAccess as string)) {
    next("/noAuth");
  }
  next();
});
